package com.auto.movil;

import java.io.FileOutputStream;
import java.io.IOException;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class RegistrarUsuarioActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.registrar_usuario );
        
        Spinner spinnerTipoDoc = (Spinner)findViewById( R.id.spinnerTipoDoc );
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.tiposDoc, android.R.layout.simple_spinner_item );
        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
        spinnerTipoDoc.setAdapter( adapter );
        
        
        
        
        
        Button butAgregarVehiculo = ( Button )findViewById( R.id.butAgregarVehiculo );
        butAgregarVehiculo.setOnClickListener( new View.OnClickListener( )
        {
            public void onClick( View v )
            {
                Intent myIntent = new Intent(v.getContext( ), RegistrarVehiculoActivity.class);
                startActivityForResult(myIntent, 0);
                
                
            }
        });
        
        Button butRegistrarme = ( Button )findViewById( R.id.butRegistrarme );
        butRegistrarme.setOnClickListener( new View.OnClickListener( )
        {
            public void onClick( View v )
            {
                try
                {
                    Intent record = new Intent( v.getContext( ), ListaRecordatoriosActivity.class );
                    almacenarUsuarioEnArchivo( );
                    startActivity( record );
                    finish( );
                }
                catch( IOException e )
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }          
            }
        });

        Button butSalir = ( Button )findViewById( R.id.butSalir );
        butSalir.setOnClickListener( new View.OnClickListener( )
        {
            public void onClick( View v )
            {
                finish();
            }
        });
        
    }
   
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == 0) {
            String vehiculo = data.getExtras( ).getString( Constants.marca )+ " "+
            data.getExtras( ).getString( Constants.modelo )+ " "+
            data.getExtras( ).getString( Constants.placa );
            
            LinearLayout layVehi = (LinearLayout)findViewById( R.id.layMisVehiculos );
            TextView txtVehiculo = new TextView( this );
            txtVehiculo.setText( vehiculo );
            txtVehiculo.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
            
            layVehi.addView( txtVehiculo );
            
            try
            {
                almacenarCarrosEnArchivo( data );
            }
            catch( IOException e )
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    
    private void almacenarCarrosEnArchivo(Intent data) throws IOException
    {
        FileOutputStream fos = openFileOutput(Constants.cars_file, Context.MODE_PRIVATE);
        String marca = data.getExtras( ).getString( Constants.marca );
        String tipo = data.getExtras( ).getString( Constants.tipoVehi );
        String modelo = data.getExtras( ).getString( Constants.modelo );
        String anio = data.getExtras( ).getString( Constants.anioFabricacion );
        String ciudad = data.getExtras( ).getString( Constants.ciudadMatricula );
        String placa = data.getExtras( ).getString( Constants.placa );
        
        String archivo = marca+";"+tipo+";"+modelo+";"+anio+";"+ciudad+";"+placa;
        fos.write(archivo.getBytes( ));
        fos.close();
    }
    
    private void almacenarUsuarioEnArchivo() throws IOException
    {
        FileOutputStream fos = openFileOutput(Constants.users_file, Context.MODE_PRIVATE);
        String nombre = ((EditText)findViewById( R.id.txtNombre )).getText( ).toString( );
        String tipoDoc = ((Spinner)findViewById( R.id.spinnerTipoDoc )).getSelectedItem( ).toString( );
        String numDoc = ((EditText)findViewById( R.id.txtNumDoc )).getText( ).toString( );
        String email = ((EditText)findViewById( R.id.txtEmail )).getText( ).toString( );
        String archivo = nombre+";"+tipoDoc+";"+numDoc+";"+email;
        fos.write(archivo.getBytes( ));
        fos.close();
    }
}